package com.maya.scoresUtility;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.maya.R;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ScoreItemAdapter extends BaseAdapter 
{
    private final Context context;
    private final List<GameScores> scores;

    class GameScores 
    {
    	private String name;
    	private int score;
    	private List<Score> scores;
	
    	public int getScore() 
    	{
			return score;
		}
		public void setScore(int score) 
		{
			this.score = score;
		}
    	public String getName() 
    	{
			return name;
		}
		public void setName(String name) 
		{
			this.name = name;
		}
		public List<Score> getScores() 
		{
			return scores;
		}
		public void setScores(List<Score> scores) 
		{
			this.scores = scores;
		}
    }

    public ScoreItemAdapter(Context context, List<Score> scores) 
    {
        this.context = context;
        
        Map<String, GameScores> scoresMap = new HashMap<String, GameScores>();
        for(Score score: scores) 
        {
        	if (!scoresMap.containsKey(score.getGroup())) 
        	{
        		GameScores gameScores = new GameScores();
        		gameScores.setScore(score.getScore());
        		gameScores.setName(score.getGroup());
        		gameScores.setScores(new ArrayList<Score> ());

        		scoresMap.put(score.getGroup(), gameScores);
        	}
        	     	
        	scoresMap.get(score.getGroup()).getScores().add(score);
        }

        this.scores = new ArrayList<GameScores>(scoresMap.values());
        
        Collections.sort(this.scores, new Comparator<GameScores> () 
        {
        	public int compare(GameScores score1, GameScores score2) 
        	{
        		if((score1.getScore())>(score2.getScore()))	return 1;
        		else if ((score1.getScore())<(score2.getScore())) return -1;
        		
        		else return 0;
        	}
        });
    }

	public int getCount() 
	{
		return this.scores.size();
	}

	public Object getItem(int position) 
	{
		return this.scores.get(position);
	}

	public long getItemId(int position) 
	{
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) 
	{
		GameScores gameScores = this.scores.get(position);

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout view = (LinearLayout)inflater.inflate(R.layout.scoreitem, null, false);

		List<Score> highScores = gameScores.getScores();
		
		TextView textViewName1 = ((TextView) view.findViewById(R.id.score_1_name));
		TextView textViewName2 = ((TextView) view.findViewById(R.id.score_2_name));
		TextView textViewName3 = ((TextView) view.findViewById(R.id.score_3_name));
		TextView textViewName4 = ((TextView) view.findViewById(R.id.score_4_name));
		TextView textViewName5 = ((TextView) view.findViewById(R.id.score_5_name));
		TextView textViewName6 = ((TextView) view.findViewById(R.id.score_6_name));
		TextView textViewName7 = ((TextView) view.findViewById(R.id.score_7_name));
		TextView textViewName8 = ((TextView) view.findViewById(R.id.score_8_name));
		TextView textViewName9 = ((TextView) view.findViewById(R.id.score_9_name));
		TextView textViewName10 = ((TextView) view.findViewById(R.id.score_10_name));
		TextView textViewName11 = ((TextView) view.findViewById(R.id.score_11_name));
		TextView textViewName12 = ((TextView) view.findViewById(R.id.score_12_name));
			
		TextView textViewScore1 = ((TextView) view.findViewById(R.id.score_1));
		TextView textViewScore2 = ((TextView) view.findViewById(R.id.score_2));
		TextView textViewScore3 = ((TextView) view.findViewById(R.id.score_3));
		TextView textViewScore4 = ((TextView) view.findViewById(R.id.score_4));
		TextView textViewScore5 = ((TextView) view.findViewById(R.id.score_5));
		TextView textViewScore6 = ((TextView) view.findViewById(R.id.score_6));
		TextView textViewScore7 = ((TextView) view.findViewById(R.id.score_7));
		TextView textViewScore8 = ((TextView) view.findViewById(R.id.score_8));
		TextView textViewScore9 = ((TextView) view.findViewById(R.id.score_9));
		TextView textViewScore10 = ((TextView) view.findViewById(R.id.score_10));
		TextView textViewScore11 = ((TextView) view.findViewById(R.id.score_11));
		TextView textViewScore12 = ((TextView) view.findViewById(R.id.score_12));
		
		TextView textViewDate1 = ((TextView) view.findViewById(R.id.score_1_date));
		TextView textViewDate2 = ((TextView) view.findViewById(R.id.score_2_date));
		TextView textViewDate3 = ((TextView) view.findViewById(R.id.score_3_date));
		TextView textViewDate4 = ((TextView) view.findViewById(R.id.score_4_date));
		TextView textViewDate5 = ((TextView) view.findViewById(R.id.score_5_date));
		TextView textViewDate6 = ((TextView) view.findViewById(R.id.score_6_date));
		TextView textViewDate7 = ((TextView) view.findViewById(R.id.score_7_date));
		TextView textViewDate8 = ((TextView) view.findViewById(R.id.score_8_date));
		TextView textViewDate9 = ((TextView) view.findViewById(R.id.score_9_date));
		TextView textViewDate10 = ((TextView) view.findViewById(R.id.score_10_date));
		TextView textViewDate11 = ((TextView) view.findViewById(R.id.score_11_date));
		TextView textViewDate12 = ((TextView) view.findViewById(R.id.score_12_date));
		
		TableRow row1 = (TableRow) view.findViewById(R.id.score_1_row);
		TableRow row2 = (TableRow) view.findViewById(R.id.score_2_row);
		TableRow row3 = (TableRow) view.findViewById(R.id.score_3_row);
		TableRow row4 = (TableRow) view.findViewById(R.id.score_4_row);
		TableRow row5 = (TableRow) view.findViewById(R.id.score_5_row);
		TableRow row6 = (TableRow) view.findViewById(R.id.score_6_row);
		TableRow row7 = (TableRow) view.findViewById(R.id.score_7_row);
		TableRow row8 = (TableRow) view.findViewById(R.id.score_8_row);
		TableRow row9 = (TableRow) view.findViewById(R.id.score_9_row);
		TableRow row10 = (TableRow) view.findViewById(R.id.score_10_row);
		TableRow row11 = (TableRow) view.findViewById(R.id.score_11_row);
		TableRow row12 = (TableRow) view.findViewById(R.id.score_12_row);
		
		TextView [] textViewNames = {textViewName1, textViewName2, textViewName3, textViewName4, textViewName5,	textViewName6, textViewName7, textViewName8, textViewName9, textViewName10,	textViewName11, textViewName12};
		TextView [] textViewScores = {textViewScore1, textViewScore2, textViewScore3, textViewScore4, textViewScore5, textViewScore6, textViewScore7, textViewScore8, textViewScore9, textViewScore10, textViewScore11, textViewScore12};
		TextView [] textViewDates = {textViewDate1, textViewDate2, textViewDate3, textViewDate4, textViewDate5,	textViewDate6, textViewDate7, textViewDate8, textViewDate9, textViewDate10,	textViewDate11, textViewDate12};
		
		TableRow [] rows = {row1, row2, row3, row4, row5, row6, row7, row8, row9, row10, row11, row12};
		
		for(int i=0; i<textViewNames.length; i++) 
		{
			if (highScores.size()>i) 
			{	
				Score score = highScores.get(i);
				
				textViewNames[i].setTextColor(Color.LTGRAY);
				textViewScores[i].setTextColor(Color.LTGRAY);			
				textViewDates[i].setTextColor(Color.LTGRAY);
	
				textViewNames[i].setText(getPlayersDisplayName(score.getPlayer()));
				textViewScores[i].setText(String.format("%04d", score.getScore()));			
				textViewDates[i].setText(score.getDateAsString());
			}
			else 
			{
				rows[i].setVisibility(View.GONE);
			}
		}

		return view;
	}

	private final int MAX_ALLOWED_NAME_LENGTH = 20;
	
	private String getPlayersDisplayName(String name) 
	{
		if (name.length() > MAX_ALLOWED_NAME_LENGTH) 
		{
			return name.substring(0, MAX_ALLOWED_NAME_LENGTH) + ".. ";
		}
		else 
		{
			String delimiter = "";
			for(int i=name.length(); i<13; i++) 
			{
				delimiter += " ";
			}
			return name + delimiter;
		}
	}

}
